package com.shouhuobao.take.recharge;

import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.tools.TextWatcherExt;
import com.shouhuobao.station.R;

/**
 * 短信充值
 * 
 * @author coffee<br>
 *         2017年6月8日下午8:33:33
 */
public class SmsReChargeActivity extends BaseActivity2 implements OnClickListener {

	private EditText mInput;
	private TextView mSmsRemain;// 剩余数量
	private TextView mSmsPrice; // 单价
	private Button mSubmit;
	// 剩余条数、单价
	private String[] dataArr;

	@Override
	protected void findViewById() {
		setContentView(R.layout.sms_rechage);
		super.findViewById();
		getAppTitle().setCommonTitle("短信充值");
		//
		String data = getIntent().getStringExtra("data");
		dataArr = data.split(",");

		mSmsRemain = (TextView) findViewById(R.id.sms_number);
		mSmsRemain.setText(String.format("当前剩余数量: %s条", dataArr[0]));
		mSmsPrice = (TextView) findViewById(R.id.sms_price);
		mSmsPrice.setText(String.format("单价: %s元/条", dataArr[1]));

		mSubmit = (Button) findViewById(R.id.submit);
		setButtonEnableStyle(mSubmit, false);
		mSubmit.setOnClickListener(this);
		//
		mInput = (EditText) findViewById(R.id.sms_input);
		mInput.addTextChangedListener(new TextWatcherExt() {
			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					setButtonEnableStyle(mSubmit, true);
				} else {
					setButtonEnableStyle(mSubmit, false);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			showLoadingDialog(null);
			String smsCount = mInput.getText().toString();
			AppHttp.getInstance().takeSmsOrderSubmit(smsCount);
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.take_sms_order_submit:
			cancelLoadingDialog();
			final ReqResult<String> result = JSON.parser(msg.obj, String.class);
			if (checkLoginStatus(result)) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Intent intent = new Intent();
						intent.setClass(getContext(), SmsPayActivity.class);
						intent.putExtra("number", mInput.getText().toString());
						intent.putExtra("remain", dataArr[0]);
						intent.putExtra("price", dataArr[1]);
						intent.putExtra("orderId", result.getResult());
						startActivity(intent);
					}
				});
			} else {
				showToast(result.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}

}
